package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.OrderDetailDao;
import com.fduss.artStore.entity.OrderDetail;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by alex on 28/06/2017.
 */
public class OrderDetailDaoImpl implements OrderDetailDao {
    private JdbcUtil util;

    public OrderDetailDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public OrderDetail add(OrderDetail orderDetail) {
        Connection conn = util.getConnection();
        //language=MySQL
        String sql = "INSERT INTO `art`.`orderdetails` " +
                "(`OrderDetailsID`, `ArtWorkID`, `OrderID`, `FrameID`, `MattID`, `GlassID`) " +
                "VALUES (?, ?, ?, ?, ?, ?)";
        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, orderDetail.getOrderDetailId());
            pst.setInt(2,orderDetail.getArtworkID());
            pst.setInt(3, orderDetail.getOrderId());
            pst.setInt(4, orderDetail.getFrameID());
            pst.setInt(5, orderDetail.getMattID());
            pst.setInt(6, orderDetail.getGlassID());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return flag ? orderDetail : null;
    }

    @Override
    public OrderDetail update(OrderDetail orderDetail) {
        Connection conn = util.getConnection();
        String sql = "UPDATE `art`.`orderdetails` " +
                "SET `ArtWorkID`=?, `OrderID`=?, `FrameID`=?, `MattID`=?, `GlassID`=? " +
                "WHERE `OrderDetailsID`=?";
        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, orderDetail.getArtworkID());
            pst.setInt(2, orderDetail.getOrderId());
            pst.setInt(3, orderDetail.getFrameID());
            pst.setInt(4, orderDetail.getMattID());
            pst.setInt(5, orderDetail.getGlassID());
            pst.setInt(6, orderDetail.getOrderDetailId());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }

        return flag ? orderDetail : null;
    }

    @Override
    public void deleteByOrder(int orderId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`orderdetails` WHERE `OrderID`=?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, orderId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public OrderDetail getByOrder(int orderId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`orderdetails` WHERE `OrderID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        OrderDetail orderDetail = null;
        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, orderId);
            rs = pst.executeQuery();
            if (rs.next()) {
                orderDetail = new OrderDetail();
                orderDetail.setOrderDetailId(rs.getInt("OrderDetailsID"));
                orderDetail.setOrderId(rs.getInt("OrderID"));
                orderDetail.setArtworkID(rs.getInt("ArtWorkID"));
                orderDetail.setFrameID(rs.getInt("FrameID"));
                orderDetail.setGlassID(rs.getInt("GlassID"));
                orderDetail.setMattID(rs.getInt("MattID"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return orderDetail;
    }
}
